package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.common.R;
import com.example.demo.entity.Meeting;
import com.example.demo.entity.MeetingRoom;
import com.example.demo.service.MeetingRoomService;
import com.example.demo.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/MeetingRoom")
@CrossOrigin
public class MeetingRoomController {
    @Autowired
    private MeetingRoomService meetingRoomService;
    @Autowired
    private MeetingService meetingService;

    //@Resource
    //private MeetingRoomMapper meetingRoomMapper;
    /**
     * 对所有会议室进行查询
     * @return 返回meetingroom的集合
     */
    @GetMapping("/getList")
    public R<List<MeetingRoom>> getMeetingRoomList(){
        List<MeetingRoom> list = meetingRoomService.list();
        return R.success(list);
    }

    @PostMapping("/getOkList")
    public R<List<MeetingRoom>> getOkRoomList(@RequestBody Meeting meeting){
        List<String> btwRoom = meetingService.getBtwTime(meeting);
        QueryWrapper<MeetingRoom> queryWrapper = new QueryWrapper<>();
                queryWrapper.notIn("meetingroom_id",btwRoom)
                .ge("meetingroom_size",meeting.getMeetingPnumber());
        if(btwRoom.isEmpty()){
            QueryWrapper<MeetingRoom> nullQW = new QueryWrapper<>();
            nullQW.ge("meetingroom_size",meeting.getMeetingPnumber());
            return  R.success(meetingRoomService.list(nullQW));
        }
        return R.success(meetingRoomService.list(queryWrapper));
    }

    @PostMapping("/AddRoom")
    public R<String> AddRoom(@RequestBody MeetingRoom meetingRoom){
        System.out.print(meetingRoom.toString());
        meetingRoomService.save(meetingRoom);
        return R.success("新建会议室成功");
    }

    @PutMapping("/UpdateRoom")
    public R<String> UpdateRoom(@RequestBody MeetingRoom meetingRoom){
        System.out.print(meetingRoom.toString());
        UpdateWrapper<MeetingRoom> updateWrapper = new UpdateWrapper<>();
        //UpdateWrapper<MeetingRoom> updateWrapper = Wrappers.update();
        String MeetingRoomId=meetingRoom.getMeetingroomId();
        updateWrapper.eq("meetingroom_id",MeetingRoomId);
        meetingRoomService.update(meetingRoom,updateWrapper );
        return R.success("修改会议室成功");
    }

    @PutMapping("/DeleteRoom")
    public R<String> DeleteRoom(@RequestBody MeetingRoom meetingRoom){
        System.out.print(meetingRoom.toString());
        QueryWrapper<MeetingRoom> wrapper = new QueryWrapper<>();
        String MeetingRoomId=meetingRoom.getMeetingroomId();
        wrapper.eq("meetingroom_id",MeetingRoomId);
        meetingRoomService.remove(wrapper);
        return R.success("删除会议室成功");
    }
}
